package net.adetech.cjg.innerservice.controller;

import com.alibaba.fastjson.JSONObject;
import net.adetech.cjg.innerservice.entity.Bzhzrw;
import net.adetech.cjg.innerservice.mapper.BzhzrwDao;
import net.adetech.cjg.innerservice.mapper.ZsDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
public class BzhzrwController {

    @Autowired
    BzhzrwDao bzhzrwDao;

    @Autowired
    ZsDao zsDao;

    @RequestMapping(value = "/cjg/getBzhzrw",produces = "application/json")
    public String getBzhzrw(){
        String ip = this.getClientIp();
        Bzhzrw bzhzrw  = this.bzhzrwDao.getBzhzrwByIp(ip);
        return JSONObject.toJSONString(bzhzrw);
    }

    @RequestMapping(value = "/cjg/updateBzhzrw",produces = "application/json")
    public String updateBzhzrwzt(String id,String rwzt,String bz)
    {
        Bzhzrw bzhzrw = new Bzhzrw();
        bzhzrw.setId(id);
        bzhzrw.setRwzt(rwzt);
        bzhzrw.setBz(bz);
        bzhzrwDao.updateBzhzrwzt(bzhzrw);
        return  "{result:'ok'}";
    }

    @Autowired
    private HttpServletRequest request;
    private String getClientIp() {
        String remoteAddr = "";

        if (request != null) {
            remoteAddr = request.getHeader("X-FORWARDED-FOR");
            if (remoteAddr == null || "".equals(remoteAddr)) {
                remoteAddr = request.getRemoteAddr();
            }
        }

        return remoteAddr;
    }
}
